﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace Project
{
    public class Mine : GameEntity, Renderable
    {
        protected Model model { get; set; }
        float timeElapsed = 0f;
        float maxTime = 2f;
        public Mine(Vector3 position, Quaternion orientation)
            : base(position, orientation, 1)
        {
            // don't smooth out rotation
            slerpConstant = 0.99f;

            // bouncin'
            elasticity = 0.0f;
            gravity = new Vector3(0, 0.25f, 0);
            dragConstant = 0;
            frictionConstant = 0;
            

            collisionObject = new CollisionObject(
                new AABB(new Vector3(-0.1f, -0.1f, -0.1f), new Vector3(0.1f, 0.1f, 0.1f), position),
                delegate(CollisionData data)
                {
                    if (data.entity != null)
                    {
                        data.entity.damage(10);
                        Vector3 force = new Vector3();
                        force = data.entity.position - this.position;
                        force.Normalize();
                        force *= 0.2f;
                        data.entity.applyForce(force);
                        this.kill();
                    }
                }, this
            );
        }

        public override void init(GameState gs)
        {
            base.init(gs);
            model = gs.loadAsset<Model>("models\\spiky_ball_low_res"); //TODO: new model
            gs.getScene().renderables.Add(this);
        }

        public override void destroy(GameState gs)
        {
            base.destroy(gs);
            gs.getScene().renderables.Remove(this);
        }

        public override bool update(GameTime gt, GameState gs)
        {
            SoundManager sm = gs.getSoundManager();
            if (health < 0)
            {
                sm.playSound("sounds\\boom1", 0.5f, false);
                return false;
            }

            base.update(gt, gs);
            return true;
        }

        public void render(GameTime gt, Camera camera)
        {
            Matrix[] transforms = new Matrix[model.Bones.Count];
            Matrix worldMatrix = Matrix.CreateTranslation(-position);
            model.CopyAbsoluteBoneTransformsTo(transforms);
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.PreferPerPixelLighting = true;
                    // effect.View = Matrix.CreateLookAt(Vector3.Backward * 3, Vector3.Forward, Vector3.Up);// camera.view; // *transforms[mesh.ParentBone.Index];
                    // effect.World = Matrix.Identity;// worldMatrix;
                    effect.World = worldMatrix;
                    effect.View = camera.view;
                    effect.Projection = camera.projection;
                }
                mesh.Draw();
            }
        }

        public void kill()
        {
            health = -1;
        }

    }
}
